# coding: utf8

from PySide import QtGui
from config_ui import Ui_Form
from lib.utils import tr
import os

class window(QtGui.QWidget, Ui_Form):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)

    def set_text(self, txt):
        self.plainTextEdit.setPlainText(txt)

    def get_text(self):
        return self.plainTextEdit.toPlainText().encode('utf8')

class configure:
    Name = 'Config'
    Type = 'ENHANCER'
    Transdir = os.path.abspath('%s%s%s' %(os.path.dirname(__file__), os.sep, 'trans'))
    Menus = [{
        'text': tr('Configure SmartMass'),
        'icon_size': (25, 25),
        'icon': '%s%s%s' %(os.path.dirname(__file__), os.sep, 'config-icon.png'),
        'trigger': 'config',
        'tool_tip': tr('A plugin to configure SmartMass.'),
        }]
    def __init__(self, parent = None):
        self.collector = None
        self.parent = parent
        self.functions = {
                'config': self.config,
                }
        self.win = window()
        self.win.pushButton_2.clicked.connect(self._on_apply)
        self.win.pushButton.clicked.connect(self._on_ok)

    def config(self):
        if not self.collector:
            return
        info = self.collector()
        s = open(info['settings_file']).read()
        self.win.set_text(s)
        plugins = info['plugins']
        self.win.label.setText(tr('Aailable plugins: %s.') %', '.join(plugins.keys()))
        if self.win.isHidden():
            self.win.show()

    def _on_apply(self):
        info = self.collector()
        s = self.win.get_text()
        open(info['settings_file'], 'w').write(s)
        self.parent.update_settings()

    def _on_ok(self):
        self._on_apply()
        self.win.close()
